<?php
namespace Common\Model;
use Think\Model;
class UploadFileModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_upload_file'; 
	
	public function getFileList($data){
		if(!empty($data['chatid'])){
			$map['chatid']  = $data['chatid'];
		}
		
		if(!empty($data['dynamicid'])){
			$map['dynamicid']  = $data['dynamicid'];
		}
		
		if(!empty($data['custid'])){
			$map['custid']  = $data['custid'];
		}
		
		
		$total = $this->where($map)->count();
		$fields = array('fileid','file','filesize','filehash','mimetype','dynamicid','chatid','custid','tcustid','createtime','updatetime');
		
		if(empty($data['sortField']))$data['sortField'] = "fileid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	
	public function addFile($data){
		$result = $this->add($data);
		return $result;
	}
	
	
	public function getFileById($fileid){
		$map['fileid'] = $fileid;
		$result = $this->where($map)->find();
		return $result;
	}
	
	
	public function updateFile($data){
		$map['fileid'] = $data['fileid'];
		$map['custid'] = $data['custid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function deleteFile($data){
		$map['fileid'] = $data['fileid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete($data);
		return $result;
	}
}


